#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
void* Routine(void*arg){
    //线程执行的函数，必须是void*和void*的参数对应pthread_creat,在上面可以转换
    char* msg = (char*)arg;
    int count = 5;
    while(count--){
        printf("i am %s pid:%d,ppid:%d,tid:%ld\n",msg,getpid(),getppid(),pthread_self());
        sleep(1);
    }
    return (void*)2025;
}
int main(){
    //创建线程
    pthread_t tid[5];
    for(int i = 0;i<5;i++){
        //为了传递的参数是不一样的，我们采用sprintf指定格式
        char* buffer = (char*)malloc(64);
        sprintf(buffer,"thread %d",i);
        pthread_create(&tid[i],NULL,Routine,(void*)buffer);
        printf("%s tid is %ld\n",buffer,tid[i]);
    }
    //主线程等待线程结束
    printf("i am main thread,pid:%d,ppid:%d,tid:%ld\n",getpid(),getppid(),pthread_self());
    for(int i = 0;i<5;++i){
        void* ret = NULL;
        //第二个参数是二级指针，可以把ret当做输入输出参数
        pthread_join(tid[i],&ret);
        printf("%d[%ld] thread quit ...,quit code:%d\n",i,tid[i],(int)ret);
    }
    return 0;
}